package jp.eisbahn.mgapi.api;

import java.util.ArrayList;
import java.util.List;

import jp.eisbahn.mgapi.http.HttpResponse;

/**
 * This is the base class to change the response to the model instance.
 * @author Yoichiro Tanaka
 */
public abstract class Response {

    /** The collection to have model objects. */
    private List<Model> entries;

    /**
     * Initialize this instance.
     */
    public Response() {
        super();
    }

    /**
     * Set the list collection to have model instances.
     * @param newEntries The list collection to have model instance.
     */
    protected final void setEntries(final List<Model> newEntries) {
        entries = newEntries;
    }

    /**
     * Add the model object.
     * @param entry The model object.
     */
    protected final void addEntry(final Model entry) {
        if (entries == null) {
            entries = new ArrayList<Model>();
        }
        entries.add(entry);
    }

    /**
     * Retrieve the collection to have model instances.
     * @param <T> The class extended Model class.
     * @return The collection to have model instances.
     */
    @SuppressWarnings("unchecked")
    public final <T extends Model> List<T> getEntries() {
        return (List<T>) entries;
    }

    /**
     * Retrieve the model object.
     * @param <T> The class extended Model class.
     * @return The model instance. If this have two or more model objects,
     * returned the first model object.
     */
    @SuppressWarnings("unchecked")
    public final <T extends Model> T getEntry() {
        if (entries == null || entries.size() == 0) {
            return null;
        } else {
            return (T) entries.get(0);
        }
    }

    /**
     * Parse and change the response to model object.
     * This sub class must implement this method.
     * @param httpResponse The response object to have the information
     * from mixi server.
     */
    public abstract void parseResponse(HttpResponse httpResponse);

}
